package com.fs.web.yz.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fs.common.pojo.CheckNote;
import com.fs.common.pojo.Order;
import com.fs.common.pojo.Product;
import com.fs.common.pojo.User;
import com.fs.common.vo.OrderVo;
import com.fs.common.yzMapper.CheckNoteMapper;
import com.fs.common.yzMapper.OrderMapper;
import com.fs.common.yzMapper.ProductMapper;
import com.fs.web.base.enums.CodeEnum;
import com.fs.web.base.utils.ApplicationUtils;
import com.fs.web.base.utils.DateUtils;
import com.fs.web.base.utils.PageHeper;
import com.fs.web.base.vo.DataGrid;
import com.fs.web.base.vo.JsonResponse;
import com.fs.wechat.constants.WXConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author DeveloperKK
 * @desc 订单管理
 */
@Slf4j
@Controller
@RequestMapping("/console/order/rewrite")
public class OrderController {


    @Autowired
    private CheckNoteMapper checkNoteMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private OrderMapper orderMapper;

    /**
     * 创建待支付订单
     * @param request
     * @param productId
     * @return
     */
    @ResponseBody
    @RequestMapping("/createPayOrder")
    public JsonResponse createOrder(HttpServletRequest request, String productId){
        try {
            if (StringUtils.isNotBlank(productId)) {
                Product product = productMapper.selectById(productId);
                if (product != null) {
                    Order order = getOrder(product, request.getRemoteAddr());
                    orderMapper.insert(order);
                    return JsonResponse.success(order);
                }
            }
        }catch (Exception e){

            e.printStackTrace();
            log.error(e.getMessage());
        }
        return JsonResponse.error(CodeEnum.EX999);
    }

    /**
     * 查询订单管理信息
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/list")
    public JsonResponse getOrder(HttpServletRequest request){
        try {
            User user = ApplicationUtils.getUser().getUser();
            Page page = PageHeper.pageRequest(request);
            Map<String, Object> map = ApplicationUtils.getRequestDatas();
            String userId = null;
            if (!user.isSysAdm()) {  // 管理员返回全部信息,个人用户只返回个人信息
                userId = user.getId();
            }
            Page<OrderVo> orderAll = orderMapper.findOrderAll(page, map, userId);
            DataGrid dataGrid = DataGrid.gridBuilder(orderAll);
            return JsonResponse.success(dataGrid);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return JsonResponse.error(CodeEnum.EX999);
        }
    }


    /**
     * 获取问卷详细信息
     * @param request
     * @param checkId
     * @return
     */
    @ResponseBody
    @RequestMapping("/getCheckNote")
    public JsonResponse getCheckNote(HttpServletRequest request,String checkId){
        try {
            CheckNote checkNote = checkNoteMapper.selectById(checkId);
            return JsonResponse.success(checkNote);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return JsonResponse.error(CodeEnum.EX999);
        }
    }

    private Order getOrder(Product product,String ipAddress){
        Order order = new Order();
        order.setIpAddress(ipAddress);
        order.setCheckNumber(product.getCount());
        order.setOrderName(product.getName());
        order.setTotalFee(product.getPrice());
        order.setTradeType(WXConstants.NATIVE);
        /**
         * SUCCESS—支付成功
         * REFUND—转入退款
         * NOTPAY—未支付
         * CLOSED—已关闭
         * REVOKED—已撤销（付款码支付）
         * USERPAYING--用户支付中（付款码支付）
         * PAYERROR--支付失败(其他原因，如银行返回失败)
         */
        order.setOrderStatus("NOTPAY");
        order.setOrderType(1);//订单类型 0支付 1充值
        order.setPayType(1);//支付类型 0微信小程序支付 1微信扫码支付
        order.setProductId(product.getId());
        order.setOutTradeNo(DateUtils.curDate("yyyyMMddHHmmssSSS"));
        order.setCreaterId(ApplicationUtils.getUser().getUser().getId());
        return order;
    }


}
